home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / ddj0897.zip / uthash.asc < prev    next >
Text File  |  1997-06-20  |  3KB  |  104 lines

  1. _Designing C++ Interfaces to C-Language Libraries_
  2. by Larry E. Baker, Jr.
  3.  
  4.  
  5. Listing One
  6. typedef struct _HT_LIST {        // list of pointers
  7.   struct _HT_LIST *next;
  8.   void            *item;
  9. } HT_LIST;
  10.  
  11. typedef struct _HASH {        // array of lists, hash/compare functions
  12.   int   size;
  13.   HT_LIST         **buckets;
  14.   int             (*hash)(void *);
  15.   int             (*compare)(void *, void *);
  16. } HASHTABLE;
  17.  
  18.  
  19. Listing Two
  20. HASHTABLE *
  21. htcreate(                      /* create a new hash table */
  22.   int size,                            /* # of buckets in table */
  23.   int (*hash_function)(void*),         /* user hash function */
  24.   int (*search_function)(void*,void*)  /* user search function */
  25. );
  26.  
  27. int
  28. htdestroy(                       /* destroy hash table */
  29.   HASHTABLE * hash_table,              /* existing hash table */
  30.   int   flags                          /* HT_FREE_REC|0 */
  31. );
  32.  
  33. int
  34. htinsert(                      /* insert a new entry */
  35.   HASHTABLE   * hash_table,            /* existing hash table */
  36.   void    * record                     /* record ptr to insert */
  37. );
  38.  
  39. int
  40. htdeleterec(                     /* delete a table entry */
  41.   HASHTABLE * hash_table,              /* existing hash table */
  42.   void    * record,                    /* record or template */
  43.   int   flags                      /* HT_FREE_REC|0 */
  44. );
  45.  
  46. int
  47. htsearch(                        /* search and/or remove */
  48.   HASHTABLE * hash_table,              /* existing hash table */
  49.   void    * templ,                     /* search template */
  50.   void    ** record,                   /* &ptr for record found */
  51.   int   flags                          /* HT_FREE_REC|HT_REPLACE|HT_DELETE|0 */
  52. );
  53.  
  54. void
  55. htstats(                           /* print out hash tbl stats */
  56.   HASHTABLE * hash_table               /* existing hash table */
  57. );
  58.  
  59. /* for htwalk, note that the function takes two arguments.  The    */
  60. /* first is a pointer to the object found in the hash table; the   */
  61. /* second is an argument passed in the void * arg pointer.         */
  62. /* this is done so an argument can be passed "through" the ht walk */
  63. /* function to the user-supplied "func" function.                  */
  64.  
  65. void
  66. htwalk(                            /* walk through the table */
  67.   HASHTABLE * hash_table,              /* existing hash table */
  68.   void  (*func)(void *, void*),        /* function to apply */
  69.   void  * arg                          /* user argument to function */
  70. );
  71.  
  72.  
  73. Listing Three
  74. template <class T> class HTable
  75. {
  76.   public:
  77.      HTable(
  78.         int size,
  79.         int destroyFlags,
  80.         int (*hashFunction)(T*),
  81.         int (*compareFunction)(T*, T*)
  82.      );
  83.      HTable(HASHTABLE * ht);
  84.      ~HTable();
  85.  
  86.      int insert(T& item);
  87.      int deleterec(T& itemTemplate, int flags);
  88.      int search(T& itemTemplate, T ** foundItem, int flags);
  89.      void stats();
  90.      void walk(void (*walkFunction)(T&, void*), void * walkArg);
  91.  
  92.      int rc;
  93.  
  94.   protected:
  95.  
  96.      HASHTABLE * ht;
  97.      int dFlags;
  98.  
  99.      static void HTable::walkDestructor( T * pT, void * arg);
  100. };
  101.  
  102.  
  103.  
  104.